package com.freewater.javabase.demo.mutithread.threadpool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 固定大小线程池
 */
public class FixedThreadPoolDemo {

    public static void main(String[] args) {
        // 创建一个固定大小的线程池:
        final ExecutorService es = Executors.newFixedThreadPool(4);
        for (int i = 0; i < 6; i++) {
            es.submit(new Task("" + i));
        }
        // 告诉线程池，没有更多任务需要添加到它的内部队列，并且一旦完成所有等待的工作就应当关闭:
        es.shutdown();
    }

    // 任务类
    static class Task implements Runnable {

        private final String name;

        public Task(String name) {
            this.name = name;
        }

        @Override
        public void run() {
            System.out.println("start task " + name);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
            System.out.println("end task " + name);
        }
    }
}
